/**
 * 
 */
package org.bspv.pharma.web.bean.node;

import java.util.ArrayList;
import java.util.List;

import org.bspv.pharma.web.bean.node.visitor.Visitor;

/**
 * @author guillaume
 *
 */
public abstract class AbstractNode implements Node {
	
	protected Long id;
	protected Long parentId;
	protected List<Node> children = new ArrayList<Node>();
	protected String name;
	
	protected Visitor visitor;

	public List<Node> getChildren() {
		return this.children;
	}

	public Long getId() {
		return this.id;
	}
	
	/**
	 * @return the parentId
	 */
	public Long getParentId() {
		return parentId;
	}

	/**
	 * @param parentId the parentId to set
	 */
	public void setParentId(Long parentId) {
		this.parentId = parentId;
	}

	public void setChildren(List<Node> children) {
		if (children == null) {
			this.children.clear();
		} else {
			this.children = children;
		}
	}

	/**
	 * @return the name
	 */
	public String getName() {
		return name;
	}

	/**
	 * @param name the name to set
	 */
	public void setName(String name) {
		this.name = name;
	}
	
	public Node searchNode(Long id, Class<? extends Node> _class) {
		Node res = null;
		if (this.id.equals(id) && this.getClass().equals(_class)) {
			res = this;
		} else {
			for (Node subNode : children) {
				Node tempRes = subNode.searchNode(id, _class);
				if (tempRes != null) {
					res = tempRes;
				}
			}
		}
		return res;
	}

	/* (non-Javadoc)
	 * @see org.bspv.pharma.webapp.bean.node.Node#accept(org.bspv.pharma.webapp.bean.node.visitor.Visitor)
	 */
	public void accept(Visitor visitor) {
		this.visitor = visitor;
	}

	/**
	 * @return the visitor
	 */
	public Visitor getVisitor() {
		return visitor;
	}
	
	/* (non-Javadoc)
	 * @see java.lang.Object#toString()
	 */
	@Override
	public String toString() {
		return name;
	}

	/* (non-Javadoc)
	 * @see java.lang.Comparable#compareTo(java.lang.Object)
	 */
	public int compareTo(Node o) {
		//methode par défaut : tri alphabétique sur le nom
		return this.getName().compareTo(o.getName());
	}
	
	
}
